import Vue from "vue";
import VueRouter from "vue-router";
import AdIndex from "../views/Admin/AdIndex.vue";
import Login from "../views/index/index/login.vue";
import priceDetail from "../views/operate/commodity/commodityprice/detail.vue";

Vue.use(VueRouter);
const routes = [
  {
    path: "/",
    component: Login,
    redirect: "/login",
  },
  {
    path: "/login",
    name: "login",
    component: Login,
    title: "登录",
  },
  {
    path: "/adIndex",
    name: "adIndex",
    component: AdIndex,
    title: "我爱报销",
    children: [
      {
        path: "/adIndex/operate/commodity/commodityprice/priceDetail/:id",
        name: "priceDetail",
        component: priceDetail,
        title: "详情",
      },
    ],
  },
];

import config from "../../public/config";
var menu = config.get("menu");
var adIndex = routes.find((item) => {
  return item.name == "adIndex";
});
var arr = [];
function dataurl(menu) {
  for (let i = 0; i < menu.length; i++) {
    if (menu[i].data_url) {
      arr.push(menu[i].data_url);
    } else {
      if (menu[i]._child) {
        dataurl(menu[i]._child);
      }
    }
  }
}
if (menu) {
  dataurl(menu);
}

arr.forEach((value, index) => {
  arr[index] = { path: value, component: () => import("../views/" + value) };
});

adIndex.children = adIndex.children.concat(arr);

const router = new VueRouter({
  mode: "history",
  routes,
});
router.$addRoutes = (params) => {
  router.matcher = new VueRouter({ mode: "history" }).matcher;
  router.addRoutes(params);
};

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

export default router;
